<template>
  <div>
    <div class="gva-form-box">
      <el-form
        :model="formData"
        ref="elFormRef"
        label-position="right"
        :rules="rule"
        label-width="80px"
      >
        <el-form-item label="产品ID:" prop="prodId">
          <el-input v-model.number="formData.prodId" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="商品名称:" prop="prodName">
          <el-input v-model="formData.prodName" :clearable="true" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="店铺id:" prop="shopId">
          <el-input v-model.number="formData.shopId" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="原价:" prop="oriPrice">
          <el-input-number
            v-model="formData.oriPrice"
            :precision="2"
            :clearable="true"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="现价:" prop="price">
          <el-input-number
            v-model="formData.price"
            :precision="2"
            :clearable="true"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="简要描述,卖点等:" prop="brief">
          <el-input
            v-model="formData.brief"
            :clearable="true"
            placeholder="请输入简要描述,卖点等"
          />
        </el-form-item>
        <el-form-item label="详细描述:" prop="content">
          <el-input v-model="formData.content" :clearable="true" placeholder="请输入详细描述" />
        </el-form-item>
        <el-form-item label="商品主图:" prop="pic">
          <el-input v-model="formData.pic" :clearable="true" placeholder="请输入商品主图" />
        </el-form-item>
        <el-form-item label="商品图片，以,分割:" prop="imgs">
          <el-input
            v-model="formData.imgs"
            :clearable="true"
            placeholder="请输入商品图片，以,分割"
          />
        </el-form-item>
        <el-form-item label="默认是1，表示正常状态, -1表示删除, 0下架:" prop="status">
          <el-input v-model.number="formData.status" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="商品分类:" prop="categoryId">
          <el-input v-model.number="formData.categoryId" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="销量:" prop="soldNum">
          <el-input v-model.number="formData.soldNum" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="总库存:" prop="totalStocks">
          <el-input v-model.number="formData.totalStocks" :clearable="true" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="配送方式json见TransportModeVO:" prop="deliveryMode">
          // 此字段为json结构，可以前端自行控制展示和数据绑定模式 需绑定json的key为
          formData.deliveryMode 后端会按照json的类型进行存取
          {{ formData.deliveryMode }}
        </el-form-item>
        <el-form-item label="运费模板id:" prop="deliveryTemplateId">
          <el-input
            v-model.number="formData.deliveryTemplateId"
            :clearable="true"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
  import { createProduct, updateProduct, findProduct } from '@/plugin/Shop/api/product'

  defineOptions({
    name: 'ProductForm',
  })

  // 自动获取字典
  import { getDictFunc } from '@/utils/format'
  import { useRoute, useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import { ref, reactive } from 'vue'

  const route = useRoute()
  const router = useRouter()

  const type = ref('')
  const formData = ref({
    prodId: undefined,
    prodName: '',
    shopId: undefined,
    oriPrice: 0,
    price: 0,
    brief: '',
    content: '',
    pic: '',
    imgs: '',
    status: undefined,
    categoryId: undefined,
    soldNum: undefined,
    totalStocks: undefined,
    deliveryMode: {},
    deliveryTemplateId: undefined,
  })
  // 验证规则
  const rule = reactive({})

  const elFormRef = ref()

  // 初始化方法
  const init = async () => {
    // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findProduct({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
  }

  init()
  // 保存按钮
  const save = async () => {
    elFormRef.value?.validate(async (valid) => {
      if (!valid) return
      let res
      switch (type.value) {
        case 'create':
          res = await createProduct(formData.value)
          break
        case 'update':
          res = await updateProduct(formData.value)
          break
        default:
          res = await createProduct(formData.value)
          break
      }
      if (res.code === 0) {
        ElMessage({
          type: 'success',
          message: '创建/更改成功',
        })
      }
    })
  }

  // 返回按钮
  const back = () => {
    router.go(-1)
  }
</script>

<style></style>
